<template>
	<view class="container">
		<view class="head-box">
			<view class="top">
				<view class="left">
					<view class="img-box">
						<image class="img" src="/static/img/my/avatar.png">
						</image>
					</view>
					<view class="name">
						{{userStore.accountInfo?.username || '张世博'}}
					</view>
				</view>
				<view class="right" @click="changeCart">
					<view class="code">
						{{userStore.nowCart?.hphm || '川A·12345'}}
					</view>
					<Button2 v-if="userStore.nowCart?.energyType === '燃油'" class="type">燃油</Button2>
					<Button2 v-else class="type green">新能源</Button2>
					<image class="change-icon" src="/static/img/common/change.png"></image>
				</view>
			</view>
			<view class="buttons">
				<ButtonBox class="button-box" :btnArr="materialArr" @btnClick="changeMaterialStatus"></ButtonBox>
			</view>
			<view class="select-date-box" v-if="nowMaterialStatus === '需求日历'">
				<!-- tab切换 -->
				<view class="tabsBox">
					<up-tabs :activeStyle="{
				fontSize:'20px',
			    color: '#2E87FF',
			    fontWeight: 'bold',
			    transform: 'scale(1.05)'
			}" :inactiveStyle="{
				fontSize:'20px',
			    color: '#0E1624',
			    transform: 'scale(1)'
			}" :list="list1" @click="clickItem" :current='tabsValue'></up-tabs>

				</view>
				<!-- 日历 -->
				<view class="tabsRl" @click="show=true">
					<text>日历</text>
					<image class="icon" src="/static/next.png"></image>
				</view>
			</view>
		</view>
		<!-- 订单 -->
		<scroll-view scroll-y class="scroll-box" :class="nowMaterialStatus !== '需求日历' && 'scroll-box2'"
			@scrolltolower="loadMore">
			<block v-if="nowMaterialStatus === '需求日历'">
				<needItem v-for="item in listData" :key="item.id" :info="item" @refresh="initPage()"></needItem>
				<up-empty mode="list" text='还没有数据' :show='listData.length==0' textSize='25' iconSize='25' width='200'
					height='200' icon="http://cdn.uviewui.com/uview/empty/list.png">
				</up-empty>
			</block>
			<block v-if="nowMaterialStatus === '我的报名'">
				<signUpItem v-for="item in listData" :key="item.id" :info="item" @refresh="initPage()"></signUpItem>
				<up-empty mode="list" text='还没有数据' :show='listData.length==0' textSize='25' iconSize='25' width='200'
					height='200' icon="http://cdn.uviewui.com/uview/empty/list.png">
				</up-empty>
			</block>
		</scroll-view>
		<!-- 搜索 -->
		<ShopCar @click="search" v-if="nowMaterialStatus === '我的报名'" />
		<!-- <view class="searchObj draggable" @click="search" :style="{right: x + 'rpx', top: y + 'rpx'}"
			v-if="nowMaterialStatus === '我的报名'" @touchmove="handleTouchMove" @touchend="handleTouchEnd">
			<text class="txt">搜索</text>
		</view> -->

		<up-calendar :show="show" :defaultDate="defaultDateMultiple" :mode="mode" @confirm="confirm"@close='close'></up-calendar>
			
			
	</view>
</template>

<script setup lang="ts">
	import { reactive, ref, onMounted, watchEffect } from 'vue';
	import dayjs from 'dayjs';
	import ButtonBox from '@/components/buttonBox/buttonBox.vue';
	import Button2 from '@/components/button2/button2.vue';
	import needItem from './components/needItem.vue'
	import signUpItem from './components/signUpItem.vue'
	import ShopCar from './components/ShopCar.vue'
	import { orderPage } from "@/api/drivers/orderTaking.js"
	import { onShow } from '@dcloudio/uni-app'
	import { useConsumptionStore, useUserStore } from '@/stroes';

	const x = ref(0);
	const y = ref(1000);

	const startX = ref(0);
	const startY = ref(0);
	// 仓库
	const userStore = useUserStore()

	// 改变车辆
	const changeCart = () => {
		uni.navigateTo({
			url: '/pages/drivers/selectCart/selectCart?type=1'
		})
	}
	const consumptionStore = useConsumptionStore()
	const materialArr = ['需求日历', '我的报名']
	// 当前物料状态
	const nowMaterialStatus = ref('需求日历')
	// 日历
	const show = ref(false);
	// 显示下标
	const tabsValue = ref(0)
	const mode = ref('single');


	//默认当天
	const defaultDateMultiple = [dayjs().format('YYYY-MM-DD')];
	// 分页
	const page = ref({
		currentPage: 1, // 当前页数
		pageSize: 10, // 每页显示多少条
	});

	// 加载状态
	const status = ref('loadmore');
	const loadingText = ref('努力加载中');
	const loadmoreText = ref('轻轻上拉');
	const nomoreText = ref('实在没有了');


	// 列表
	const listData = ref([]);
	// 修改物料状态
	const changeMaterialStatus = (btn : string) => {
		nowMaterialStatus.value = btn
		tabsValue.value = 0
		consumptionStore.searchInfo = {}
		weekDta.value = ''
		initPage()
		console.log(btn)

	}

	// 创建响应式数据  
	const list1 = ref([]);
	function getDay() {
		let today = dayjs();
		let weekday = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
		let daysOfWeek = [];
		daysOfWeek.push({
			date: today.format('YYYY-MM-DD'),
			name: weekday[today.day()]
		})
		for (let i = 1; i <= 4; i++) {
			const day = dayjs().add(i, 'day');

			daysOfWeek.push({
				date: day.format('YYYY-MM-DD'), // 日期格式化为YYYY-MM-DD
				name: weekday[day.day()]  // 星期几，0-6 分别代表周日到周六
			});
		}
		list1.value = [{ name: "全部", date: '' }, ...daysOfWeek]

	}
	const weekDta = ref('')
	// 定义方法  
	function clickItem(item, index) {
		tabsValue.value = item.index
		listData.value = []
		weekDta.value = item.date
		initPage()
	}
	// 加载更多
	function loadMore() {

	}
	// 搜索
	function search() {
		uni.navigateTo({
			url: '/pages/drivers/enrollSearch/enrollSearch'
		})
	}

	const confirm = (e) => {
		console.log(e);
		weekDta.value = e[0]
		show.value = false

		initPage()
	};

	function close() {
		show.value = false
	}

	// 加载数据
	function initPage() {
		let obj = {
			status: nowMaterialStatus.value == '需求日历' ? '' : '1',
			signupDate: weekDta.value,
			startingPlace: consumptionStore.searchInfo.startingPlace,
			terminusPlace: consumptionStore.searchInfo.transportCompanyName,
			startTime: consumptionStore.searchInfo.startTime,
			endTime: consumptionStore.searchInfo.endTime,
			plateNos: consumptionStore.searchInfo.checkCartTypes || []
		}
		uni.hideLoading()
		uni.showLoading({
			title: '数据加载中...'
		})
		console.log("传过去的参数", obj)
		orderPage(obj).then(res => {
			console.log("订单数据", res)
			if (res.data.code == 0) {
				listData.value = res.data.data
				// console.log('ooooooooo', listData.value);
				uni.hideLoading()
			} else {
				console.log("失败")
				uni.hideLoading()
			}
		})
	}

	const handleTouchMove = (event) => {
		console.log(event)
		// 获取触摸移动时的坐标
		const touch = event.touches[0];
		const currentX = touch.clientX;
		const currentY = touch.clientY;

		// 计算移动的偏移量
		const offsetX = currentX - startX.value;
		const offsetY = currentY - startY.value;
	
		// 更新按钮位置
		x.value += offsetX;
		y.value += offsetY;

		// 更新开始坐标为最新的坐标
		startX.value = currentX;
		startY.value = currentY;
	}
	function handleTouchEnd(event) {
		startX.value = 0;
		startY.value = 0;
	}

	onShow(() => {
		if (userStore.nowPageId === 0) {
			initPage()

		}
		console.log("consumptionStore", consumptionStore.searchInfo)
	})
	onMounted(() => {
		getDay()
		initPage()

	});
</script>

<style scoped lang="scss">
	.container {
		height: 100vh;
		background: #EAF0F9;
		position: relative;

		.head-box {
			padding-top: 95rpx;
			padding-bottom: 10rpx;
			// border-radius: 0rpx 0rpx 32rpx 32rpx;
			// background: linear-gradient(59deg, #438DFF 0%, #056FFF 100%);
			background: linear-gradient(180deg, #2E87FF 0%, #F0F3F8 100%);

			.top {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 10rpx;

				.left {
					display: flex;
					align-items: center;

					.img-box {
						margin-left: 32rpx;
						width: 112rpx;
						height: 112rpx;
						border: 2rpx solid #fff;
						border-radius: 50%;

						.img {
							width: 100%;
							height: 100%;
							border-radius: 50%;
						}
					}



					.name {
						margin: 0rpx 32rpx 0rpx 24rpx;
						font-size: 32rpx;
						color: #fff;
					}
				}

				.right {
					flex: 1;
					display: flex;
					justify-content: center;
					align-items: center;
					padding: 26rpx 36rpx 26rpx 26rpx;
					border-radius: 48rpx 0rpx 0rpx 48rpx;
					background-color: #fff;
					white-space: nowrap;

					.code {
						font-size: 36rpx;
						font-weight: 500;
						color: $primary-color;
					}

					.type {
						margin-left: 18rpx;
						margin-right: 32rpx;
						padding: 0 18rpx;
						line-height: 40rpx;
					}

					.change-icon {
						width: 42rpx;
						height: 42rpx;
					}
				}
			}

			.buttons {
				margin: 0rpx 32rpx;
			}

			.select-date-box {
				display: flex;
				justify-content: space-between;
				align-items: center;

				.tabsBox {
					padding: 16rpx;
				}

				.tabsRl {
					width: 110rpx;
					height: 70rpx;
					text-align: center;
					border-radius: 32rpx 0rpx 0rpx 32rpx;
					line-height: 35rpx;
					background: #FFFFFF;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #0E1624;
					font-style: normal;
					text-transform: none;
					display: flex;
					align-items: center;
					justify-content: center;

					.icon {
						width: 18rpx;
						height: 9rpx;
						margin-left: 10rpx;
					}
				}

			}
		}

		.searchObj {
			position: absolute;
			width: 116rpx;
			height: 116rpx;

			background: url('@/static/img/car/search.png') no-repeat no-repeat center/ 100% 100%;
			box-sizing: border-box;

			.txt {
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 24rpx;
				color: #2E87FF;
				line-height: 28rpx;
				text-align: left;
				font-style: normal;
				text-transform: none;
				position: relative;
				left: 34rpx;
				top: 62rpx;
			}
		}
	}

	.scroll-box {
		padding: 20rpx 32rpx 32rpx;
		height: 1060rpx;
		box-sizing: border-box;

		// background-color: skyblue;
		&.scroll-box2 {
			height: 1140rpx;
		}
	}
</style>